package ATM.frame;

import ATM.pojo.UserDO;
import ATM.service.UserService;
import ATM.service.impl.UserServiceImpl;
import ATM.util.Util;
import ATM.util.UtilSql1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LogJpanel extends JPanel implements ActionListener {
    private ATMJframe atmJframe;
    private JButton btnLog;
    private Icon background = Util.getimagesIcon("/images/buttonLand.jpg");
    private int count = 3; //剩余登录机会
    private JTextField textFieldId;
    private JPasswordField textFieldPassWord;
    private UserService userService = new UserServiceImpl();


    public LogJpanel(ATMJframe atmJframe) {
        this.atmJframe = atmJframe;
        this.setLayout(null);
        this.setOpaque(false);
        btnLog = new JButton("", background);
        btnLog.setBounds(420, 400, 140, 50);
        this.add(btnLog);

        JLabel Id = new JLabel("账号：");
        Id.setBounds(300, 200, 100, 50);
        Id.setFont(new Font("宋体", Font.PLAIN, 18));
        this.add(Id);

        textFieldId = new JTextField();
        textFieldId.setBounds(350, 200, 300, 50);
        this.add(textFieldId);

        JLabel passWord = new JLabel("密码：");
        passWord.setBounds(300, 270, 100, 50);
        passWord.setFont(new Font("宋体", Font.PLAIN, 18));
        this.add(passWord);

        textFieldPassWord = new JPasswordField();
        textFieldPassWord.setBounds(350, 270, 300, 50);
        this.add(textFieldPassWord);


        btnLog.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == btnLog) {
            String id = textFieldId.getText();
            String pwd = String.valueOf(textFieldPassWord.getPassword());


            UserDO user = userService.getByusernameAndPwd(id, pwd);
            if (user != null) {
                atmJframe.setUser(user);
                atmJframe.switchPanel("menu");
                return;
            }

//        for (int i = 0; i < atmJframe.getUsersArrayList().size(); i++) {
//            if (atmJframe.getUsersArrayList().get(i).getId().equals(textFieldId.getText())
//                    && atmJframe.getUsersArrayList().get(i).getPassWord().equals(String.valueOf(textFieldPassWord.getPassword()))) {
//                atmJframe.switchPanel("menu");
//
//                atmJframe.setUser(atmJframe.getUsersArrayList().get(i));
//                count=3;
//
//                return;
//            }
//        }
            count--;
            if (count == 0) {
                JOptionPane.showMessageDialog(this, "机会用完，卡被吞掉");
                System.exit(0);
            }
            JOptionPane.showMessageDialog(this, "登录失败还剩" + count + "次机会");
        }
    }
}//last
